/**
 *@Description 服务配置
 *@Author chenxq
 *@Date 201516-01-13
 */
var serviceConfigCtrl  = new XFApp.client();
XFApp.client.prototype.serviceConfig = {
    successCode : "10000",
    page : null,
    urlItems : {
        queryServiceConfigURL : basePath+"/serviceConfig/getConfig",
        saveServiceConfigURL : basePath+"/serviceConfig/save",
        delServiceConfigURL : basePath+"/serviceConfig/delete"
    },
    queryConfig: function(){
        _this = this;
        var customerCode = $("#searchForm input[name='customerCode']").val();
        var username = $("#searchForm input[name='username']").val();
        if(customerCode == '' && username == ''){
            //window.webDialogUtil.alert("提示","请输入查询条件");
            return;
        }
        $.ajax( {
            url:_this.urlItems.queryServiceConfigURL,
            data:$('#searchForm').serialize(),
            type:'get',
            cache:false,
            dataType:'json',
            success:function(data) {
                var result = data.result;
                if(result == null){
                    $("#configDiv input[type=checkbox]").prop("checked", false);
                    $("#search-error").text("客户配置信息不存在，请先进行服务配置");
                    return ;
                }else{
                    $("#configDiv").show();
                }
                var routes = result.logisticsRoutes || null;
                var reports = result.reportChannels|| null;
                var storages = result.storages|| null;
                $("#id").val(result.id);
                if(result.customerCode != null){
                    $("#customerCode").val(result.customerCode);
                }else{
                    $("#customerCode").val($("#searchForm input[name=customerCode]").val());
                }
                if(result.username != null){
                    $("#username").val(result.username);
                }else{
                    $("#username").val($("#searchForm input[name=username]").val());
                }
                if(storages != null && storages.length > 0){
                    var selectCount = 0;
                    for(var i = 0; i < storages.length; i++){
                        if(storages[i]){
                            selectCount++;
                            $("#storage_"+storages[i].id).prop("checked", "checked");
                        }
                    }
                    if($("#storageList input").length == selectCount){
                        $("#checkbox-storage").prop("checked", "checked");
                    }
                }
                if(routes != null && routes.length > 0){
                    var selectCount = 0;
                    for(var i = 0; i < routes.length; i++){
                        if(routes[i]) {
                            $("#route_" + routes[i].id).prop("checked", "checked");
                            selectCount++;
                        }
                    }
                    if($("#routeList input").length == selectCount){
                        $("#checkbox-route").prop("checked", "checked");
                    }
                }
                if(reports != null && reports.length > 0){
                    var selectCount = 0;
                    for(var i = 0; i < reports.length; i++){
                        if(reports[i]) {
                            selectCount++;
                            $("#report_" + reports[i].id).prop("checked", "checked");
                        }
                    }
                    if($("#reportList input").length == selectCount){
                        $("#checkbox-report").prop("checked", "checked");
                    }
                }
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    save : function(){
        _this = this;
        var storageIds = [],routeIds = [],reportIds=[];
        $("#storageList input").each(function(){
            if($(this).prop("checked")){
                var id = $(this).prop("id").split("_")[1];
                storageIds.push(id);
            }
        });
        $("#routeList input").each(function(){
            if($(this).prop("checked")){
                var id = $(this).prop("id").split("_")[1];
                routeIds.push(id);
            }
        });
        $("#reportList input").each(function(){
            if($(this).prop("checked")){
                var id = $(this).prop("id").split("_")[1];
                reportIds.push(id);
            }
        });
        var data = {
            id:$("#id").val(),
            customerCode:$("#customerCode").val(),
            username:$("#username").val(),
            storageIds:storageIds.join(","),
            routeIds: routeIds.join(","),
            reportIds:reportIds.join(",")
        }
        $.ajax( {
            url:_this.urlItems.saveServiceConfigURL,
            data:data,
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                window.webDialogUtil.alert("提示",data.msg);
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    selectOption:function(eleName) {
        $("#checkbox-"+eleName).on("click",function(){
            if($("#checkbox-"+eleName).prop("checked")){
                $("#"+eleName+"List input").prop("checked",true);
            }else{
                $("#"+eleName+"List input").prop("checked",false);
            }
        });
        $("#"+eleName+"List input").on("click",function(){
            var selectCount = 0;
            $("#"+eleName+"List input").each(function(index){
                if($(this).prop("checked")){
                    selectCount++;
                }
            });
            if(selectCount == $("#"+eleName+"List input").length){
                $("#checkbox-"+eleName).prop("checked",true);
            }else{
                $("#checkbox-"+eleName).prop("checked",false);
            }
        })
    },
    event : function(){
        _this = this;
        $("#searchButton").on("click",function(){
            serviceConfigCtrl.serviceConfig.queryConfig();
        });
        $("#saveButton").on("click",function(){
            serviceConfigCtrl.serviceConfig.save();
        });
        serviceConfigCtrl.serviceConfig.queryConfig();
        serviceConfigCtrl.serviceConfig.selectOption("storage");
        serviceConfigCtrl.serviceConfig.selectOption("route");
        serviceConfigCtrl.serviceConfig.selectOption("report");
    }
};

$(function(){
    serviceConfigCtrl.serviceConfig.event();
});